<?php
/**
 * @Author: kaihui.wang
 * @Contact  hpuwang@gmail.com
 * @Version: 1.0.0
 * @Date: 2020/11/2 10:14
 */

namespace Eduxplus\WebsiteBundle\Controller\Web;


use Eduxplus\CoreBundle\Lib\Base\BaseHtmlController;
use Eduxplus\WebsiteBundle\Service\TutorialService;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\Annotation\Route;

class TutorialController extends BaseHtmlController
{

    
    public function indexAction($category=0,$page=1, TutorialService $tutorialService){
        $page = $page?$page:1;
        $pageSize = 40;
        list($pagination, $list) = $tutorialService->getTutorialList($category, $page, $pageSize);
        $tutorialCategory = $tutorialService->getAllFirstCategory();
        $categoryInfo = $tutorialService->getCategoryById($category);
        $data = [];
        $data["tutorialCategory"] = $tutorialCategory;
        $data['list'] = $list;
        $data['pagination'] = $pagination;
        $data['category'] = $category;
        $data['route'] = "app_tutorial";
        $data['seoTitle'] = $categoryInfo && isset($categoryInfo['name'])?$categoryInfo['name']."-教程":"教程";
        $data['seoDescr']="教程列表";
        $seoKw = "";
        if($tutorialCategory){
            foreach($tutorialCategory as $cate){
                $seoKw .= $cate['name'].",";
            }
        }
        $seoKw = trim($seoKw, ",");
        $data['seoKw']= $seoKw;
        return $this->render("@WebsiteBundle/tutorial/index.html.twig", $data);
    }
    
    public function detailAction($id,$chapterId,TutorialService $tutorialService){
        $tutorialService->viewNumIncre($id);
        $detail = $tutorialService->getById($id, $chapterId);
        if(!$detail || !$detail["status"]){
            return $this->render("@WebsiteBundle/404.html.twig");
        }
        $data = [];
        $data['detail'] = $detail;
        $data['chapterId'] = $detail['content']?$detail['content']["id"]:0;
        $data['seoTitle'] = $detail['content']?$detail['content']['name']:$detail['name'];
        $data['seoDescr']=$detail['descr'];
        $data['seoKw']=$detail['content']?$detail['content']['tags']:"";
        $data['route'] = "app_tutorial";
        $uid = $this->getUid();
        $data['fav'] = [];
        if($uid){
            $data['fav'] = $tutorialService->getFav($id, $uid);
        }
        return $this->render("@WebsiteBundle/tutorial/detail.html.twig", $data);
    }

    public function doFavAction($id, TutorialService $tutorialService){
        $uid = $this->getUid();
        $check = $tutorialService->getFav($id, $uid);
        if($check){
            $tutorialService->delFav($id, $uid);
            $tutorialService->favNumDecre($id);
            return $this->responseSuccess(["fav"=>0], "取消收藏成功!");
        }else{
            $tutorialService->addFav($id, $uid);
            $tutorialService->favNumIncre($id);
            return $this->responseSuccess(["fav"=>1],"收藏成功!");
        }
    }

    public function doClearFailureAction(TutorialService $tutorialService){
        $uid = $this->getUid();
        $tutorialService->clearFailure($uid);
        return $this->responseMsgReload("操作成功!");
    }

}
